package com.hk.order.controller;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.hk.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RefreshScope
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    @Value("${order.app.name}")
    String appName;
    @Value("${redis.host}")
    String redisHost;
    @Value("${redis.port}")
    Integer redisPort;

    @GetMapping("{userId}/{productId}/{count}")
    public Boolean createOrder(@PathVariable("userId")String userId,
                               @PathVariable("productId")String productId,
                               @PathVariable("count")Integer count){
        System.out.println("配置中心参数 appName = " + appName);
        System.out.println("配置中心redis参数: host = " + redisHost+" , port = " + redisPort);
        return orderService.saveOrder(userId,productId,count);
    }

    @GetMapping("/test")
    @SentinelResource(value = "testResource",blockHandler = "deadTest" ,
            blockHandlerClass = OrderController.class)
    public String test(@RequestParam(value = "pid",required = false)String pid,
                       @RequestParam(value = "count",required = false)String
                               count){
        System.out.println("购买商品的id为："+pid+" , 数量："+count);
        return "test";
    }
    //也可以将熔断降级的自定义方法抽取到单独的一个类中，此处省略....
    public static String deadTest(String pid, String count, BlockException
            blockException){
        System.out.println("deadTest---购买商品的id为："+pid+" , 数量："+count+" , 异常： "+ blockException);
        return "dead_test";
    }
}
